package com.qf.controller;

import com.github.pagehelper.PageInfo;
import com.qf.bean.Position;
import com.qf.bean.PositionDept;
import com.qf.bean.Positiongroup;
import com.qf.dao.PositionDeptMapper;
import com.qf.dao.PositionMapper;
import com.qf.dao.PositiongroupMapper;
import com.qf.service.PositionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * 28/11/2019
 * Echo
 * qmxc
 * Alibaba
 */
@Controller
public class PositionController {

    @Autowired
    private PositiongroupMapper positiongroupMapper;

    @Autowired
    private PositionService positionService;

    @RequestMapping("/zhaopin/demo1/getAll")
    //这里是职位发布的分页模糊查方法
    public String showPage(ModelMap modelMap, @RequestParam(defaultValue = "1") Integer index, Position position) {
        List<Positiongroup> positiongroups = positiongroupMapper.getAll();
        modelMap.addAttribute("group", positiongroups);
        PageInfo<Position> info = positionService.getAll(index, position);
        List<Position> list = info.getList();
//        modelMap.addAttribute("positionname",position.getPositionname());
//        modelMap.addAttribute("positiongroupid",position.getPositiongroupid());
        modelMap.addAttribute("pos", position);
        modelMap.addAttribute("info", info);
        return "zhaopin/demo1/list";
    }

    @Autowired
    private PositionDeptMapper positionDeptMapper;

    //这里是职位发布的增加模块的下拉框信息显示
    @RequestMapping("/zhaopin/demo1/findSelect")
    public String findSelect(ModelMap map) {
        List<PositionDept> deptList = positionDeptMapper.getAll();
        map.addAttribute("depts", deptList);
        List<Positiongroup> positiongroupList = positiongroupMapper.getAll();
        map.addAttribute("poss", positiongroupList);
        return "zhaopin/demo1/add";
    }

    //这里是职位发布的添加模块
    @RequestMapping("/zhaopin/demo1/add")
    public void add(Position position, HttpServletResponse response) {
        Integer i = positionService.add(position);
        response.setContentType("text/html;charset=utf-8");
        PrintWriter writer = null;
        try {
            writer = response.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (i > 0) {
            writer.print("<script>alert('发布成功');location.href='/zhaopin/demo1/getAll';</script>");
        } else {
            writer.print("<script>alert('发布失败');location.href='/zhaopin/demo1/getAll';</script>");
        }

    }

    //这里是职位发布的删除模块
    @RequestMapping("/zhaopin/demo1/delete")
    public void delete(Integer pid, HttpServletResponse response) {
        response.setContentType("text/html;charset=utf-8");
        PrintWriter writer = null;
        try {
            writer = response.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Integer i = positionService.delete(pid);
        if (i > 0) {
            writer.print("<script>alert('删除成功');location.href='/zhaopin/demo1/getAll';</script>");
        } else {
            writer.print("<script>alert('删除失败');location.href='/zhaopin/demo1/getAll';</script>");
        }
    }

    //这里做修改的回显
    @RequestMapping("/zhaopin/demo1/showBack")
    public String showBack(ModelMap map, Integer pid) {
        List<PositionDept> deptList = positionDeptMapper.getAll();
        ModelMap depts = map.addAttribute("depts", deptList);
        Position position = positionService.showBack(pid);
        map.addAttribute("pos", position);
        List<Positiongroup> positiongroupList = positiongroupMapper.getAll();
        map.addAttribute("groups", positiongroupList);

        return "zhaopin/demo1/update";
    }

    @RequestMapping("/zhaopin/demo1/update")
    public void update(Position position, HttpServletResponse response) {
        response.setContentType("text/html;charset=utf-8");
        PrintWriter writer = null;
        try {
            writer = response.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Integer i = positionService.update(position);
        if (i > 0) {
            writer.print("<script>alert('修改成功');location.href='/zhaopin/demo1/getAll';</script>");
        } else {
            writer.print("<script>alert('修改失败');location.href='/zhaopin/demo1/getAll';</script>");
        }
    }
}


